package com.ibt.controller;

import java.util.ArrayList;

import java.util.Date;
import java.util.Iterator;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.ibt.dao.BugEntryDAO;
import com.ibt.dao.EmployeeDAO;
import com.ibt.dao.ModuleDAO;
import com.ibt.dao.ProjectEntryDAO;
import com.ibt.entity.BugEntry;
import com.ibt.entity.Business;
import com.ibt.entity.Employee;
import com.ibt.entity.Login;
import com.ibt.entity.Module;
import com.ibt.entity.ProjectEntry;
@Controller
@RequestMapping("/module")
public class ModuleController {
		@RequestMapping( "/show.do") 
		public ModelAndView insert(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Login  Ulogin=(Login)session.getAttribute("user");
			Business bus=(Business)session.getAttribute("business");
			System.out.println("the bi=usiness object is"+bus.getBusinessId());
				ProjectEntryDAO oprD=new ProjectEntryDAO();
				List<ProjectEntry> opr=null;
				List<Employee> emplist=null;
				try{
					opr=(List<ProjectEntry>)oprD.findAll();
						System.out.println("opportunity list is "+opr);
						 emplist=(List<Employee>)EmployeeDAO.findAll(bus.getBusinessId());
						 System.out.println("the employee list is"+emplist);
				}catch(NullPointerException e){
					e.printStackTrace();
				}
				  model = new ModelAndView("/jsp/addBuilds.jsp");
					model.addObject("projectlist",opr);
					model.addObject("emplist",emplist);
					/*model.addObject("submenu", "sourcingInfo");
					model.addObject("menuName","sourcingInfo");*/
					return model;
		}

	

	@RequestMapping(value="/add.do",method = RequestMethod.POST)
	public ModelAndView userEntry(@RequestParam("cby") String cby, 
			@RequestParam("ccode") String ccode,
			@RequestParam("odate") String odate, 
			@RequestParam("ddate") String ddate,
			@RequestParam("pname") String pname, 
			@RequestParam("mname") String mname,
			@RequestParam("section") String section, 
			@RequestParam("lng") String lang,
			@RequestParam("bcode") String bcode,
			@RequestParam("bname") String bname, 
			@RequestParam("bstatus") String bstat,
			@RequestParam("bdesc") String bdesc, 
			ModelAndView model,HttpSession session) {
		Business bus=(Business)session.getAttribute("business");
		Login  Ulogin=(Login)session.getAttribute("user");
		
		//BugEntry bEntry=new BugEntry(cby,ccode,new Date(),pname,mname,section,lang,bcode,bname,bstat);
		
     try {
		//BugEntryDAO.insert(bEntry);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		System.out.println("in x0029 searching  BusinessUserid " );
		model.setViewName("/jsp/BugInfo.jsp");
		return model;

}
	@RequestMapping(value="/moduleSearch.do")
	public @ResponseBody  String moduleSearch ( 
			@RequestParam("pname") String projname,
			ModelAndView model,HttpSession session) throws Exception{
		Login  Ulogin=(Login)session.getAttribute("user");
		Business bus=(Business)session.getAttribute("business");
		StringBuilder modulelist=new StringBuilder();
		
	List<Module>  mlist= new ArrayList<Module>();

		StringBuilder sourcelist= new StringBuilder();
		
			mlist=ModuleDAO.getModuleList(projname.trim(),1);
		
		if (mlist!=null) {
			Iterator it = mlist.iterator();
			while (it.hasNext() == true) {
				Object ob = it.next();
				Module mod = (Module) ob;
				String qname = mod.getModuleName();
				sourcelist.append("," + qname);
			}
		}
		
		return sourcelist.toString();
     }

	
	

}
